﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;

namespace ConAppCore.Test
{
    internal class TestParaRangeManager
    {
        public static void Test()
        {
            ValueRangeManager valueRangeManager = new ValueRangeManager();
            valueRangeManager.AddTypeValueRange<TestParaRangeManagerClass>(nameof(TestParaRangeManagerClass.Count), new NumberValueRange<int>("xx", 10, 20));

            var obj = new TestParaRangeManagerClass();
            obj.Age = 18;
            obj.Count = 25;

            TypeValueRange typeParaRange1 = valueRangeManager.GetTypeValueRange<TestParaRangeManager>();
            TypeValueRange typeParaRange2 = valueRangeManager.GetTypeValueRange(typeof(TestParaRangeManagerClass));

            IValueRange paraValueRange;
            try
            {
                if (typeParaRange2.TryGetValueRange(nameof(TestParaRangeManagerClass.Age), out paraValueRange))
                {
                    paraValueRange.Validate(obj.Age, obj, null);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }

            try
            {
                if (typeParaRange2.TryGetValueRange(nameof(TestParaRangeManagerClass.Count), out paraValueRange))
                {
                    paraValueRange.Validate(obj.Count, obj, null);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }
    }

    internal class TestParaRangeManagerClass
    {
        [Required]
        [ValueRange(0, 100)]
        public int Age { get; set; }

        public int Count { get; set; }

        [ValueRange(10, 30)]
        public int Size;
    }
}
